package cn.wolfcode.rbac.web.controller.system_manage;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.query.QueryEm;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.system_manage.IDepartmentService;
import cn.wolfcode.rbac.service.system_manage.IEmployeeService;
import cn.wolfcode.rbac.service.system_manage.IRoleService;
import cn.wolfcode.rbac.util.PageResult;
import cn.wolfcode.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @version 1.0
 * @Author:Cao Zhijianu0:05:37
 */
@Controller
public class EmployeeController {

    @Autowired
    private IEmployeeService iEmployeeService;

    @Autowired
    private IDepartmentService iDepartmentService;

    @Autowired
    private IRoleService iRoleService;

    /**
     * 员工列表的显示
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:06
     * @param model :
     * @return : java.lang.String
     */
//    @RequestMapping("/employee/list")
//    @RequiredPermission({"员工列表","employee:list"})
//    public String list(Model model, QueryObject queryObject){
//
//        // 查询出数据
//        PageResult result = iEmployeeService.query(queryObject);
////        System.out.println(employees);
//        // 回显数据
//        model.addAttribute("result",result);
//
//        return "/employee/list";
//    }

    /**
     * 删除员工
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:06
     * @return : java.lang.String
     */
    @RequestMapping("/employee/delete")
    @RequiredPermission({"员工删除","employee:delete"})
    public String delete(Long id){
        iEmployeeService.delete(id);

        // 重定向列表界面
        return "redirect:/employee/list";
    }

    /**
     * 新增和编辑员工
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:14
     * @param id : 员工id
     * @return : java.lang.String
     */
    @RequestMapping("/employee/input")
    public String input(Model model, Long id){

        if(id != null){
            // 如果id不为空，则为编辑功能，编辑功能要回显数据
            // 根据员工id查找员工信息
            Employee employee = iEmployeeService.get(id);
            model.addAttribute("entity",employee);
        }

        // 把系统中所有的部门共享到界面中
        model.addAttribute("depts",iDepartmentService.listAll());

        // 把系统中所有的角色共享到界面中
        model.addAttribute("roles",iRoleService.listAll());

        // 返回到编辑界面
        return "/employee/input";
    }

    /**
     * 新增和编辑员工的保存效果
     * @Author Cao Zhijian
     * @Creed: Talk is cheap,show me the code
     * @Created: 2020/12/24 0024 21:27
     * @param employee :
     * @return : java.lang.String
     */
    @RequestMapping("/employee/saveOrUpdate")
    @RequiredPermission({"员工新增或编辑","employee:saveOrUpdate"})
    public String saveOrUpdate(Employee employee,Long[] ids){

        iEmployeeService.saveOrUpdate(employee,ids);

        return "redirect:/employee/list";
    }

    //---------------
    @RequestMapping("/employee/batchDelete")
    @ResponseBody
    public String  batchDelete(Long[] ids) {
        iEmployeeService.batchDelete(ids);
        return "success";
    }

    @RequestMapping("/employee/exportXls")
    public String exportXls(HttpServletResponse response) throws Exception {

        iEmployeeService.exportXls();
        return "redirect:/employee/list";
    }

    @RequestMapping("/employee/importXls")
    @ResponseBody
    public String importXls(HttpServletRequest req) throws Exception {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) req;
        List<MultipartFile> files = multipartHttpServletRequest.getFiles("file");
        for(MultipartFile file:files){
            iEmployeeService.importXls(file);
        }

        return "success";
    }
    @RequestMapping("/employee/list")
    @RequiredPermission({"员工列表","employee:list"})
    public String list(Model model, @ModelAttribute("qo") QueryEm qo){
        // System.out.println(qo.getKeyword()+"-------------");
        // 查询出数据
        PageResult result = iEmployeeService.query(qo);
//        System.out.println(employees);
        // 回显数据
        model.addAttribute("result",result);

        List<Department> depts=iEmployeeService.listAllDepartment();
        model.addAttribute("depts",depts);
        return "/employee/list";
    }
}
